Code:
#pragma comment( lib, "user32.lib" )
#pragma comment( lib, "olepro32.lib" )
#pragma comment( lib, "gdi32.lib" )
#pragma comment( lib, "ole32.lib" )
#pragma comment( lib, "Wininet.lib" )
#include <windows.h>
#include <Wininet.h>
#include <olectl.h>
#define COLORREF2RGB(Color) (Color & 0xff00) | ((Color >> 16) & 0xff) \
| ((Color << 16) & 0xff0000)
LPPICTURE gpPicture;
HBITMAP hbm;
HBITMAP ChangePixels(HBITMAP hBmp,COLORREF cOldColor,COLORREF cNewColor,HDC hBitmapDC)
{
HBITMAP hReturnBitmap = NULL;
if (hBmp)
{
HDC BufferDC=CreateCompatibleDC(NULL);
if (BufferDC)
{
HBITMAP hTempBitmap = (HBITMAP) NULL;
if (hBitmapDC)
if (hBmp == (HBITMAP)GetCurrentObject(hBitmapDC, OBJ_BITMAP))
{
hTempBitmap = CreateBitmap(1, 1, 1, 1, NULL);
SelectObject(hBitmapDC, hTempBitmap);
}
HGDIOBJ PreviousBuffer=SelectObject(BufferDC,hBmp);
HDC DirectDC=CreateCompatibleDC(NULL);
if (DirectDC)
{
BITMAP bm;
GetObject(hBmp, sizeof(bm), &bm);
BITMAPINFO BitmapInfo;
ZeroMemory(&BitmapInfo,sizeof(BITMAPINFO));
BitmapInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth=bm.bmWidth;
BitmapInfo.bmiHeader.biHeight=bm.bmHeight;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 32;
UINT * upPixels;
HBITMAP DirectBitmap= CreateDIBSection(DirectDC, (BITMAPINFO *)&BitmapInfo, DIB_RGB_COLORS,(void **)&upPixels, NULL, 0);
if (DirectBitmap)
{
HGDIOBJ PreviousObject=SelectObject(DirectDC, DirectBitmap);
BitBlt(DirectDC,0,0,bm.bmWidth,bm.bmHeight,BufferDC,0,0,SRCCOPY);
char cNewColorRef=COLORREF2RGB(cNewColor);
for (int iIndex = ((bm.bmWidth*bm.bmHeight) - 1); iIndex >= 0; iIndex--)
if (upPixels[iIndex] == cOldColor) upPixels[iIndex] = cNewColorRef;
SelectObject(DirectDC,PreviousObject);
hReturnBitmap=DirectBitmap;
}
DeleteDC(DirectDC);
}
if (hTempBitmap)
{
SelectObject(hBitmapDC, hBmp);
DeleteObject(hTempBitmap);
}
SelectObject(BufferDC,PreviousBuffer);
DeleteDC(BufferDC);
}
}
return hReturnBitmap;
}
HBITMAP getFromInternet(char *pURL)
{
HBITMAP hBitmap;
HINTERNET hInternet = InternetOpen( "GINA: Version 0.1", INTERNET_OPEN_TYPE_DIRECT, NULL, 0,0);
HINTERNET hFile = InternetOpenUrl(hInternet, pURL, NULL, 0, 0, 0);
DWORD sizeBuffer;
DWORD length = sizeof(sizeBuffer);
bool succeeds = HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, (LPVOID)&sizeBuffer, &length, NULL);
if (!succeeds)
{
throw "Unable to fetch map";
return (HBITMAP) NULL;
}
LPVOID pvData = NULL;
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, sizeBuffer);
pvData = GlobalLock(hGlobal);
DWORD bytesRead;
DWORD bytesToRead = sizeBuffer;
if (hFile)
{
InternetReadFile(hFile, pvData, bytesToRead, &bytesRead);
}
else
throw "Unable to grab overview image";
GlobalUnlock(hGlobal);
CloseHandle(hFile);
LPSTREAM pstm = NULL;
// create IStream* from global memory
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
// Create IPicture from image file
if (gpPicture)
gpPicture->Release();
hr = OleLoadPicture(pstm, sizeBuffer, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
pstm->Release();
gpPicture-> get_Handle ((OLE_HANDLE *) & hBitmap);
hBitmap = (HBITMAP) CopyImage (hBitmap, IMAGE_BITMAP, 0,0,0);
InternetCloseHandle(hFile);
InternetCloseHandle(hInternet);
return hBitmap;
}
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "JpegExample" ;
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
int WinSize = 500;
int Left = 200;
int Top = 100;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = NULL ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = NULL ;
RegisterClassEx (&wndclass) ;
hwnd = CreateWindow (
szAppName,
"Jpeg Example",
WS_OVERLAPPEDWINDOW,
Left,
Top ,
WinSize,
WinSize,
NULL,
NULL,
hInstance,
NULL
) ;
ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc,hMemDC;
PAINTSTRUCT ps;
BITMAP bm;
RECT MainRc;
POINT pt;
COLORREF colNew, colOld;
switch (iMsg)
{
case WM_RBUTTONDOWN:
colNew = RGB(255,255,255);
colOld = RGB(0,0,0);
hbm = ChangePixels(hbm,colOld,colNew,NULL);
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_CREATE :
hbm = getFromInternet("http://www.imperialconflict.com/images/Galaxies/Capricorn.overview.jpg");
return 0 ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd,&MainRc);
hMemDC = CreateCompatibleDC(hdc);
SelectObject(hMemDC,hbm);
GetObject(hbm,sizeof(BITMAP),&bm);
pt.x = MainRc.right;
pt.y = MainRc.bottom;
SetStretchBltMode(hdc,COLORONCOLOR);
StretchBlt(hdc,0,0,pt.x,pt.y,hMemDC,0,0,800,600,SRCCOPY);
DeleteDC(hMemDC);
EndPaint(hwnd, &ps);
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}